import java.util.ArrayList;
import java.util.Random;
/**
 *  算式基
 */
public  class BinaryOperationBase extends  FileOperation{
    private String[][] addbase = new String[100][100];
    private String[][] subbase = new String[100][100];
    private String[][] multbase = new String[100][100];
    private String[][] divibase = new String[100][100];
    ArrayList<String> mixList = new ArrayList<String>();
    ArrayList<String> addList = new ArrayList<String>();
    ArrayList<String> subList = new ArrayList<String>();
    ArrayList<String> multList = new ArrayList<String>();
    ArrayList<String> diviList = new ArrayList<String>();
    private Random random = new Random();
    public String[][] getAddBase(int max){
        for(int i = 0; i < max; i++){
            for(int j = 0; j < max; j++){
                if(i + j < max){
                    String str = i+"+"+j;
                    addbase[i][j] = str;
                    FileOperation.save(str+",","AddBase.txt");
                }
            }
        }
        return addbase;
    }
    public String[][] getSubBase(int max){
        for(int i = 0; i < max; i++){
            for(int j = 0; j < max; j++){
                if(i - j >= 0){
                    String str = i+"-"+j;
                    subbase[i][j] = str;
                    FileOperation.save(str+",","SubBase.txt");
                }
            }
        }
        return subbase;
    }
    // 存储乘法算式基
    public String[][] getMultBase(){
        for(int i = 0; i <= 20; i++){
            for(int j = 0; j <=50; j++){
                    String str = i+"-"+j;
                    multbase[i][j] = str;
                    FileOperation.save(str+",","MultBase.txt");
            }
        }
        return multbase;
    }
    // 根据MAX存储除法算式基
    public String[][] getDiviBase(Integer max){
        for(int i = 1; i < max; i++){
            for(int j = i; j < max; j++){
                    String str = j+"/"+i;
                    divibase[i][j] = str;
                    FileOperation.save(str+",","DiviBase.txt");
            }
        }
        return divibase;
    }
    public void getMixBase(){
        read(addList,"AddBase.txt");
        read(subList,"SubBase.txt");
        mixList.addAll(addList);
        mixList.addAll(subList);
        mixList.forEach(f->{
            FileOperation.save(f+",","MixBase.txt");
        });
    }
    public void showAddBase(Integer num){
        FileOperation.read(addList,"Addbase.txt");
        if (addList.size() == 0 ){System.out.println("AddBase文件为空");System.exit(0);}
        Random random = new Random();
        int i =1;
        while (i <= num){
            System.out.print(String.format("%-11s",addList.get(random.nextInt(addList.size()))));
            if (i % 5 ==0){System.out.println();}
            i++;
        }
        addList.clear();
    }
    public void showSubBase(Integer num){
        FileOperation.read(subList,"SubBase.txt");
        if (subList.size() == 0 ){System.out.println("SubBase文件为空");System.exit(0);}
        Random random = new Random();
        int i =1;
        while ( i<= num){
            System.out.print(String.format("%-11s",subList.get(random.nextInt(subList.size()))));
            if (i % 5 ==0){System.out.println();}
            i++;
        }
        subList.clear();
    }
    public void showMixBase(Integer num){
        FileOperation.read(mixList,"MixBase.txt");
        if (mixList.size() == 0 ){System.out.println("MixBase文件为空");System.exit(0);}
        Random random = new Random();
        int i =1;
        while ( i<= num){
            System.out.print(String.format("%-11s",mixList.get(random.nextInt(mixList.size()))));
            if (i % 5 ==0){System.out.println();}
            i++;
        }
        mixList.clear();
    }
    public void showMultBase(Integer num){
        FileOperation.read(multList,"MultBase.txt");
        if (multList.size() == 0 ){System.out.println("MultBase文件为空");System.exit(0);}
        Random random = new Random();
        int i =1;
        while ( i<= num){
            System.out.print(String.format("%-11s",multList.get(random.nextInt(multList.size()))));
            if (i % 5 ==0){System.out.println();}
            i++;
        }
        multList.clear();
    }
    public void showDiviBase(Integer num){
        FileOperation.read(diviList,"DiviBase.txt");
        if (diviList.size() == 0 ){System.out.println("DiviBase文件为空");System.exit(0);}
        Random random = new Random();
        int i =1;
        while ( i<= num){
            System.out.print(String.format("%-11s",diviList.get(random.nextInt(diviList.size()))));
            if (i % 5 ==0){System.out.println();}
            i++;
        }
        diviList.clear();
    }
    public String showOneAdd(){
        FileOperation.read(addList,"AddBase.txt");
        if (addList.size() == 0){System.out.println("AddBase文件为空");System.exit(0);}
        return addList.get(random.nextInt(addList.size()));
    }
    public String showOneSub(){
        FileOperation.read(subList,"SubBase.txt");
        if (subList.size() == 0 ){System.out.println("SubBase文件为空");System.exit(0);}
        return subList.get(random.nextInt(subList.size()));
    }
    public String showOneMix(){
        FileOperation.read(mixList,"MixBase.txt");
        if (mixList.size() == 0 ){System.out.println("MixBase文件为空");System.exit(0);}
        return mixList.get(random.nextInt(mixList.size()));
    }
    public void construct(Integer type){
            switch (type){
                case 1:showAddBase(50);break;
                case 2:showSubBase(50);break;
                case 3:showMultBase(50);break;
                case 4:showDiviBase(50);break;
                default: break;
            }
    }
}

